package com.example.calendertodo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.example.calendertodo.util.ToastUtil;
import com.example.calendertodo.util.TodoDbOpenHelper;
import com.lihang.chart.utils.ChartCircleItem;
import com.lihang.chart.utils.ChartHistogramItem;
import com.lihang.chart.view.ChartCircleView;
import com.lihang.chart.view.ChartHistogramView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class CountActivity extends AppCompatActivity {
    private TextView tv_start;
    private TextView tv_end,tv_complete,tv_cancel,tv_late,tv_count;
    private String startTime,endTime,Time,temp,temp1,temp_start,temp_start1,start,end;
    private int year,moon,day,hour,minute;
    private int flag;
    private Button btn_count;
    private ChartCircleView chartCircleView;
    private ChartHistogramView chartHistogramView;
    private TodoDbOpenHelper mTodoDbOpenHelper;
    private ArrayList<ChartCircleItem> items = new ArrayList<>();
    private ArrayList<ChartCircleItem> items_count = new ArrayList<>();
    private ArrayList<ChartHistogramItem> items1 = new ArrayList<>();
    private ArrayList<ChartHistogramItem> items1_count = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_count);
        getCurrentTimeFormat();
        mTodoDbOpenHelper = new TodoDbOpenHelper(this);
        initView();
        initDate();



        tv_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DatePickerDialog(CountActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {//年月日
                        int month = i1 + 1;// 此处i1从0开始，需要加一
                        if (month <= 9) {
                            temp_start = format(month);
                        }else {
                            temp_start = month + "";
                        }
                        if(i2 <= 9) {
                            temp_start1 = format(i2);
                        }else {
                            temp_start1 = i2 + "";
                        }
                        startTime = i +"-"  + temp_start + "-" + temp_start1 + "\t";
                        flag = 0;
                        Log.d("tag","------日期--------" + startTime);
                        popTimePick(startTime,flag);

                    }
                },year,moon,day).show();
            }
        });

        tv_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(CountActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {//年月日
                        int month = i1 + 1;// 此处i1从0开始，需要加一
                        if (month <= 9) {
                            temp = format(month);
                        }else {
                            temp = month + "";
                        }
                        if(i2 <= 9) {
                            temp1 = format(i2);
                        }else {
                            temp1 = i2 + "";
                        }
                        endTime = i +"-"  + temp + "-" + temp1 + "\t";
                        flag = 1;
                        popTimePick(endTime,flag);
                    }
                },year,moon,day).show();
            }
        });



        btn_count.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (items_count.size() != 0) {
                    items_count.clear();
                }
                if (items1_count.size() != 0) {
                    items1_count.clear();
                }

                String start_time = tv_start.getText().toString();
                String end_time = tv_end.getText().toString();
                if(start_time.compareTo(end_time) >= 0) {
                    ToastUtil.toastShort(CountActivity.this,"请选择正确的时间");
                    return;
                }
                int late = mTodoDbOpenHelper.queryBetweenDate(start_time,end_time,"2");
                int complete = mTodoDbOpenHelper.queryBetweenDate(start_time,end_time,"1");
                int cancel = mTodoDbOpenHelper.queryBetweenDate(start_time,end_time,"3");
                int todo = mTodoDbOpenHelper.queryBetweenDate(start_time,end_time,"0");
                items_count.add(new ChartCircleItem(late, R.color.red, "逾期"));
                items_count.add(new ChartCircleItem(complete, R.color.blue, "完成"));
                items_count.add(new ChartCircleItem(cancel, R.color.white, "取消"));
                items_count.add(new ChartCircleItem(todo, R.color.white_1, "暂未完成"));
                chartCircleView.setItems(items_count);
                items1_count.add(new ChartHistogramItem(late,R.color.red,"逾期",true));
                items1_count.add(new ChartHistogramItem(complete,R.color.blue,"完成",true));
                items1_count.add(new ChartHistogramItem(cancel,R.color.white,"取消",true));
                items1_count.add(new ChartHistogramItem(todo,R.color.white_1,"暂未完成",true));
                chartHistogramView.setItems(items1_count);
                String count = String.valueOf(mTodoDbOpenHelper.queryAllBetweenDate(start_time,end_time));
                tv_count.setText(count);
            }
        });
    }

    private void initDate() {
        tv_start.setText(start);
        tv_end.setText(end);
        int late = mTodoDbOpenHelper.queryBetweenDate(start,end,"2");
        int complete = mTodoDbOpenHelper.queryBetweenDate(start,end,"1");
        int cancel = mTodoDbOpenHelper.queryBetweenDate(start,end,"3");
        int todo = mTodoDbOpenHelper.queryBetweenDate(start,end,"0");
        items.add(new ChartCircleItem(late, R.color.red, "逾期"));
        items.add(new ChartCircleItem(complete, R.color.blue, "完成"));
        items.add(new ChartCircleItem(cancel, R.color.white, "取消"));
        items.add(new ChartCircleItem(todo, R.color.white_1, "暂未完成"));
        chartCircleView.setItems(items);
        items1.add(new ChartHistogramItem(late,R.color.red,"逾期",true));
        items1.add(new ChartHistogramItem(complete,R.color.blue,"完成",true));
        items1.add(new ChartHistogramItem(cancel,R.color.white,"取消",true));
        items1.add(new ChartHistogramItem(todo,R.color.white_1,"暂未完成",true));
        chartHistogramView.setItems(items1);
        String count = String.valueOf(mTodoDbOpenHelper.queryAllBetweenDate(start,end));
        tv_count.setText(count);
    }

    //选择具体时间
    public void popTimePick(String time,int flag) {
        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int i, int i1) {
                String hour = "",minute = "";

                if(i <= 9) {
                    hour = format(i);
                }else {
                    hour = i + "";
                }

                if(i1 <= 9) {
                    minute = format(i1);
                }else {
                    minute = i1 + "";
                }
                Time = time + hour + ":" + minute;

                if (flag == 0) { //起始时间
                    tv_start.setText(Time);
                }else if(flag == 1) {
                    tv_end.setText(Time);
                }

            }
        },hour,minute,true).show();
    }

    private void initView() {
        tv_start = findViewById(R.id.tv_start_count);
        tv_end = findViewById(R.id.tv_end_count);
        tv_count = findViewById(R.id.tv_count);
        btn_count = findViewById(R.id.btn_count);
        chartCircleView = findViewById(R.id.cv_circle);
        chartHistogramView = findViewById(R.id.cv_hg);
    }


    private String format(int date) {
        String exact_date = "0" + date;
        return exact_date;
    }

    private void getCurrentTimeFormat() { //获取当前时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd\tHH:mm");
        Date date = new Date();
        String time = simpleDateFormat.format(date);
        String[] times = time.split("\t");
        String head = times[0];
        String tail = times[1];
        start = head.substring(0,8) + "01\t" + "00:00";
        end = head.substring(0,8) + "31\t" + "24:00";
        year = Integer.parseInt(head.split("-")[0]);
        moon = Integer.parseInt(head.split("-")[1]) - 1;
        day = Integer.parseInt(head.split("-")[2]);
        hour = Integer.parseInt(tail.split(":")[0]);
        minute = Integer.parseInt(tail.split(":")[1]);
    }


    //菜单栏事件
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_count,menu); //绑定菜单
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {

            case R.id.menu_calender:
                //切换到日历视图
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                break;


            case R.id.menu_list:
                //切换到列表视图
                Intent intent1 = new Intent(this, TodoListActivity.class);
                startActivity(intent1);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}